************************************
*** FIGURE A3: Clinic-Level Outcomes by Quarter

*** TABLE A17: Dynamic Effect of EMR Adoption, Year FE Interacted with Clinic Size
*** TABLE A18-A22
************************************


***FIGURE A3

use Data/Analysis/Data_ClinicQuarters, clear

keep if QPostEMR<5 & QPostEMR>-5

forvalues l = 0/4 {
	gen L`l'event = QPostEMR==`l'
}
forvalues l = 1/4 {
	gen F`l'event = QPostEMR==-`l'
}
drop F1event

foreach y of varlist logNew logDeath {
	reghdfe `y' L*event F*event, absorb(VisitSite) cluster(VisitSite)
	estimates store e_`y'
}

foreach y of varlist logInCare {
	reghdfe `y' L*event, absorb(VisitSite) cluster(VisitSite)
	estimates store e_`y'
}


event_plot e_logNew e_logDeath e_logInCare, ///
		stub_lag(L#event L#event L#event) stub_lead(F#event F#event F#event) plottype(scatter) ciplottype(rspike) ///
		together perturb(-0.15(0.3)0.15) trimlead(4) noautolegend ///
		graph_opt( ///
			xtitle("Quarter Index") ytitle("Coefficient Estimates") xlabel(-4(1)4) ylabel(-0.25(0.25)0.5) ///
			legend(pos(6) order(1 "log(New Patients)" 3 "log(Patient Deaths)" 5 "log(Patients In Care)" ///
			) rows(1) region(style(none))) ///
		/// the following lines replace default_look with something more elaborate
			xline(-0.5, lcolor(gs8) lpattern(dash)) yline(0, lcolor(gs8)) graphregion(color(white)) bgcolor(white) ylabel(, angle(horizontal)) ///
		) ///
		lag_opt1(msymbol(O) color(cranberry)) lag_ci_opt1(color(cranberry)) ///
		lag_opt2(msymbol(D) color(navy)) lag_ci_opt2(color(navy)) ///
		lag_opt3(msymbol(S) color(black)) lag_ci_opt3(color(black)) 
graph save "Output/Figures/FigureA03.gph", replace
graph export "Output/Figures/FigureA03.png", replace




***TABLE A17

use Data/Analysis/Data_ClinicYears, clear

keep YearsPostEMR EMRyear Datey VisitSite logInCare* logDeath* logNew* logReturn* ARVInit logTBT Post large urban hospital ARVInit logTBT 

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t

forvalues l = 0/6 {
	gen L`l'event = K==`l'
}
replace L6event=1 if K>6
forvalues l = 1/6 {
	gen F`l'event = K==-`l'
}
drop F1event

cap erase Output/Tables/TableA17.xls
cap erase Output/Tables/TableA17.txt

foreach y of varlist logDeath logInCare logNew logReturn {
	reghdfe `y' F*event L*event i.large#i.t, a(i t) cluster(i)
	outreg2 using Output/Tables/TableA17.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE by Clinic Size, Y, Year FE, Y, Clinic FE, Y)
}



***TABLES A18-A21


use Data/Analysis/Data_ClinicYears, clear

gsort VisitSite YearsPostEMR


gen d = InCare if Datey==2017
by VisitSite: egen denom=min(d)
drop d

foreach x in Male Female Age_09 Age_1017 Age_1849 Age_50plus {
		gen d`x' = InCare`x' if Datey==2017
		by VisitSite: egen denom`x'=min(d`x')
		drop d`x'
}
	
	
foreach y of varlist InCare Death New Return {
	gen denom`y' = `y'/denom
	foreach x in Male Female Age_09 Age_1017 Age_1849 Age_50plus {
		gen denom`y'`x' = `y'`x'/denom`x'
	}
}


keep YearsPostEMR EMRyear Datey VisitSite Death New InCare Return ihstInCare* ihstDeath* ihstNew* ihstReturn* *logInCare* *logDeath* *logNew* *logReturn* denom* 


gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t


forvalues l = 0/6 {
	gen L`l'event = K==`l'
}
replace L6event=1 if K>6
forvalues l = 1/6 {
	gen F`l'event = K==-`l'
}
drop F1event

cap erase Output/Tables/TableA18.xls
cap erase Output/Tables/TableA18.txt

foreach x of varlist logDeath logNew {
	reghdfe `x' F*event L*event if EMRyear!=2019, a(i t) cluster(i)
		estimates store e_`x'
		testparm F*event
		outreg2 using Output/Tables/TableA18.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE, Y, Clinic FE, Y) addstat("F-test on pre-trends", r(F),"p-value of F-test", r(p))
}

foreach x of varlist logInCare logReturn {
	reghdfe `x' L*event if EMRyear!=2019, a(i t) cluster(i)
		estimates store e_`x'
		outreg2 using Output/Tables/TableA18.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event) addtext(Year FE, Y, Clinic FE, Y, "F-test on pre-trends", N/A, "p-value of F-test", N/A) 
}


cap erase Output/Tables/TableA19.xls
cap erase Output/Tables/TableA19.txt

foreach x of varlist ihstDeath ihstNew {
	reghdfe `x' F*event L*event, a(i t) cluster(i)
		estimates store e_`x'
		testparm F*event
		outreg2 using Output/Tables/TableA19.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE, Y, Clinic FE, Y) addstat("F-test on pre-trends", r(F),"p-value of F-test", r(p))
}

foreach x of varlist ihstInCare ihstReturn {
	reghdfe `x' L*event, a(i t) cluster(i)
		estimates store e_`x'
		outreg2 using Output/Tables/TableA19.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event) addtext(Year FE, Y, Clinic FE, Y, "F-test on pre-trends", N/A, "p-value of F-test", N/A) 
}

cap erase Output/Tables/TableA20.xls
cap erase Output/Tables/TableA20.txt

foreach x of varlist altlogDeath altlogNew {
	reghdfe `x' F*event L*event, a(i t) cluster(i)
		estimates store e_`x'
		testparm F*event
		outreg2 using Output/Tables/TableA20.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE, Y, Clinic FE, Y) addstat("F-test on pre-trends", r(F),"p-value of F-test", r(p))
}


foreach x of varlist altlogInCare altlogReturn {
	reghdfe `x' L*event, a(i t) cluster(i)
		estimates store e_`x'
		outreg2 using Output/Tables/TableA20.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event) addtext(Year FE, Y, Clinic FE, Y, "F-test on pre-trends", N/A, "p-value of F-test", N/A) 
}


cap erase Output/Tables/TableA21.xls
cap erase Output/Tables/TableA21.txt

foreach x of varlist denomDeath denomNew {
	reghdfe `x' F*event L*event, a(i t) cluster(i)
		estimates store e_`x'
		testparm F*event
		outreg2 using Output/Tables/TableA21.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(F*event L*event) addtext(Year FE, Y, Clinic FE, Y) addstat("F-test on pre-trends", r(F),"p-value of F-test", r(p))
}

foreach x of varlist denomInCare denomReturn {
	reghdfe `x' L*event, a(i t) cluster(i)
		estimates store e_`x'
		outreg2 using Output/Tables/TableA21.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(L*event) addtext(Year FE, Y, Clinic FE, Y, "F-test on pre-trends", N/A, "p-value of F-test", N/A) 
}

***TABLE A22

use Data/Analysis/Data_ClinicYears, clear


keep YearsPostEMR EMRyear Datey VisitSite logInCare* logDeath* logNew* logReturn* Post hospital urban large

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t

cap erase Output/Tables/TableA22.xls
cap erase Output/Tables/TableA22.txt

foreach y of varlist logDeath logDeathM logDeathF logDeathAge_* logNew logNewM logNewF logNewAge_* {
	did_imputation `y' i t Ei, horizons(0 1 2 3 4) pretrend(3) cluster(VisitSite) 
		outreg2 using Output/Tables/TableA22.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) ///
		addstat("F-test on pre-trends", e(pre_F) , "p-value of F-test", e(pre_p)) title("Borusyak's Pretrends F-test and p-values") 
}

